{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第0节：NumPy简介\n",
    "NumPy是一个开源的Python科学计算基础库，包含：\n",
    "+ 一个强大的N维数组对象ndarray\n",
    "+ 广播功能函数\n",
    "+ 整合C/C++/Fortran代码的工具\n",
    "+ 线性代数、傅里叶变换、随机数生成等功能\n",
    "\n",
    "NumPy是SciPy、Pandas等数据处理或科学计算库的基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**例1：计算$C = $$A^2$ + $B^3$，其中，A和B是一维数组**\n",
    "\n",
    "a = [0, 1, 2, 3, 4]\n",
    "\n",
    "b = [9, 8, 7, 6, 5]\n",
    "\n",
    "c = [729, 513, 347, 225, 141]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c =  [729, 513, 347, 225, 141]\n"
     ]
    }
   ],
   "source": [
    "#传统方法\n",
    "def Pysum():\n",
    "    a = [0, 1, 2, 3, 4]\n",
    "    b = [9, 8, 7, 6, 5]\n",
    "    c = []\n",
    "\n",
    "    for i in range(len(a)):\n",
    "        c.append(a[i]**2 + b[i]**3)\n",
    "    return c\n",
    "\n",
    "print(\"c = \",Pysum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c =  [729 513 347 225 141]\n"
     ]
    }
   ],
   "source": [
    "#numpy方法\n",
    "import numpy as np\n",
    "\n",
    "def npSum():\n",
    "    a = np.array([0, 1, 2, 3, 4])\n",
    "    b = np.array([9, 8, 7, 6, 5])\n",
    "    c = a**2 + b**3\n",
    "    return c\n",
    "\n",
    "print(\"c = \",npSum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----\n",
    "**使用numpy的优点**\n",
    "\n",
    "+ 去掉元素间运算所需的循环，使一维向量更像单个数据\n",
    "\n",
    "+ 设置专门的数组对象，经过优化，可以提升运算速度\n",
    "\n",
    "**一般的：**科学计算中，一个维度所有数据的类型往往相同\n",
    "+ 数组对象采用相同的数据类型，有助于节省运算和存储空间\n",
    "\n",
    "**Tips：**numpy底层由C语言实现，运算速度是Python的**10倍**以上"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**N维数组对象：ndarray**\n",
    "+ 实际数据\n",
    "+ 描述这些数据的元数据（数据维度、数据类型等）\n",
    "+ 要求所有元素类型相同（同质），数组下标从0开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([[0, 1, 2, 3, 4], [9, 8, 7, 6, 5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [9, 8, 7, 6, 5]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a #直接写上变量名，在命令行会直接显示变量信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [9 8 7 6 5]]\n"
     ]
    }
   ],
   "source": [
    "print(a) #注意 a 与 print(a)的区别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "# 第1节：ndarray基本概念**\n",
    "1. 轴（axis）:数据维度\n",
    "2. 秩（rank）:轴的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a.ndim =  2\n",
      "a.shape =  (2, 5)\n",
      "a.size =  10\n",
      "a.dtype =  int32\n",
      "a.itemsize =  4\n"
     ]
    }
   ],
   "source": [
    "# ndarry的5个基本属性\n",
    "import numpy as np\n",
    "\n",
    "# 先创建一个ndarray\n",
    "a = np.array([[0, 1, 2, 3, 4], [9, 8, 7, 6, 5]])\n",
    "\n",
    "# .ndim：秩，即轴的数量(或维度的数量)\n",
    "print(\"a.ndim = \", a.ndim)\n",
    "\n",
    "# .shape：对象的尺度，对于矩阵，n行m列\n",
    "print(\"a.shape = \", a.shape)\n",
    "\n",
    "# .size：对象元素的个数(相当于.shape中n*m的值)\n",
    "print(\"a.size = \", a.size)\n",
    "\n",
    "# .dtype：对象的元素类型\n",
    "print(\"a.dtype = \", a.dtype)\n",
    "\n",
    "# .itemsize：对象中每个元素的大小，以字节为单位\n",
    "print(\"a.itemsize = \", a.itemsize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### ndarray的元素类型（了解）\n",
    "![avatar](./image/day1-img1.PNG)\n",
    "![avatar](./image/day1-img2.PNG)\n",
    "![avatar](./image/day1-img3.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a.shape =  (2,)\n",
      "a.size =  2\n",
      "a.dtype =  object\n",
      "a.itemsize =  8\n",
      "a.ndim =  1\n"
     ]
    }
   ],
   "source": [
    "# numpy支持非同质的元素类型，但是一般情况不要使用\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([[0, 1, 2], [0, 1, 2, 3, 4]])\n",
    "\n",
    "print(\"a.shape = \", a.shape)\n",
    "\n",
    "print(\"a.size = \", a.size)\n",
    "\n",
    "print(\"a.dtype = \", a.dtype)# 元素类型：object（对象）\n",
    "\n",
    "print(\"a.itemsize = \", a.itemsize)\n",
    "\n",
    "print(\"a.ndim = \", a.ndim)# 相当于一组对象，所以维度数量为1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "# 第2节：ndarray的创建\n",
    "+ 从Python中的列表、元组等类型创建\n",
    "+ 使用NumPy中函数创建（如：arange, ones, zeros）\n",
    "+ 从字节流（raw bytes）中创建（了解）\n",
    "+ 从文件中读取特定格式创建（了解）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从Python中的列表、元组等类型创建\n",
    "import numpy as np\n",
    "\n",
    "List1 = [[1,2,3],[7,8,9]]\n",
    "Tuple1 = (1,2,3,4,5)\n",
    "\n",
    "# 从列表创建\n",
    "a = np.array(List1,dtype = np.float32)\n",
    "b = np.array(List1)\n",
    "\n",
    "# 从元组创建\n",
    "c = np.array(Tuple1)\n",
    "d = np.array(Tuple1,dtype = np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1., 2., 3.],\n",
       "        [7., 8., 9.]], dtype=float32),\n",
       " array([[1, 2, 3],\n",
       "        [7, 8, 9]]),\n",
       " array([1, 2, 3, 4, 5]),\n",
       " array([1., 2., 3., 4., 5.], dtype=float32))"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意看 a 和 b(c 和 d) 的差别（指定了 dtype 和没有指定的差别）\n",
    "a,b,c,d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([list([1, 2, 3]), list([4, 5]), (9, 8, 7)], dtype=object)"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 从 列表、元组混合 创建\n",
    "T = [\n",
    "    [1,2,3],\n",
    "    [4,5],\n",
    "    (9,8,7)\n",
    "]\n",
    "np.array(T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**从NumPy函数创建（最常用）**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.arange(n)\n",
    "# 返回数组[0,1,2,3,...,n-1]\n",
    "a = np.arange(5)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**NumPy函数1**\n",
    "+ np.ones(shape) 根据shape生成全 0 数组\n",
    "+ np.zeros(shape) 根据shape生成全 1 数组\n",
    "+ np.full(shape,val) 根据shape生成全 val 数组\n",
    "---\n",
    "**shape是元组，shape的含义**\n",
    "\n",
    "例子：\n",
    "+ 一维数组：shape=(5) => 长度为5的一维数组\n",
    "+ 二维数组：shape=(3,4) => 3x4矩阵\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1., 1., 1.],\n",
       "        [1., 1., 1.]]),\n",
       " array([[0., 0., 0.],\n",
       "        [0., 0., 0.]]),\n",
       " array([[-1, -1, -1],\n",
       "        [-1, -1, -1]]))"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.ones((2,3))\n",
    "c = np.zeros((2,3))\n",
    "d = np.full((2,3),-1)\n",
    "# 可以在后面指定 dtype 类型\n",
    "# 例如：d = np.full((2,3),-1,dtype=np.float32)\n",
    "\n",
    "b,c,d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**NumPy函数2**\n",
    "+ np.ones_like(a) 根据数组a的形状生成一个全1数组\n",
    "+ np.zeros_like(a) 根据数组a的形状生成一个全0数组\n",
    "+ np.full_like(a,val) 根据数组a的形状生成一个数组，每个元素值都是val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 1],\n",
       "        [1, 1]]),\n",
       " array([[0, 0],\n",
       "        [0, 0]]),\n",
       " array([[-2, -2],\n",
       "        [-2, -2]]))"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [\n",
    "    [1, 2],\n",
    "    [3, 4]\n",
    "]\n",
    "\n",
    "b = np.ones_like(a)\n",
    "c = np.zeros_like(a)\n",
    "d = np.full_like(a, -2)\n",
    "# 同样可以设置dtype\n",
    "# d = np.full_like(a, -2,dtype=np.float32)\n",
    "\n",
    "b,c,d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**NumPy函数3**\n",
    "+ np.linspace() 根据起止数据等间距地填充数据，形成数组\n",
    "+ np.concatenate() 将两个或多个数组合并成一个新的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 1.,  4.,  7., 10.]), array([1.  , 3.25, 5.5 , 7.75]))"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 区间[1,10]中,等距的取4个数\n",
    "a = np.linspace(1, 10, 4)\n",
    "\n",
    "# endpoint参数表示右区间是闭还是合\n",
    "# endpoint = False => 区间为[1,10)\n",
    "b = np.linspace(1, 10, 4, endpoint=False)\n",
    "\n",
    "# 可指定 dtype\n",
    "# a = np.linspace(1, 10, 4, dtype=np.int32)\n",
    "# b = np.linspace(1, 10, 4, endpoint=False, dtype=np.int32)\n",
    "a, b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5,  2,  4,  6,  8, -1, -2, -3])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将多个数组合并\n",
    "a = [1,3,5]\n",
    "b = [2,4,6,8]\n",
    "c = [-1,-2,-3]\n",
    "d = np.concatenate((a,b,c))\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**ndarray数组的变换**\n",
    "+ 改变元素类型直接设置dtype\n",
    "+ 使用相关函数进行维度变换\n",
    "---\n",
    "+ .reshape(shape) 不改变数组元素，返回一个shape形状的数组，原数组不变\n",
    "+ .resize(shape)与.reshape()功能一致，但修改原数组\n",
    "+ .swapaxes(ax1,ax2) 将数组n个维度中两个维度进行调换\n",
    "+ .flatten() 对数组进行降维，返回折叠后的一维数组，原数组不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 2, 3, 4, 5, 6, 7, 8]),\n",
       " array([[1, 2, 3, 4],\n",
       "        [5, 6, 7, 8]]))"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# (1)不改变数组元素(2)原数组不变\n",
    "# 返回一个shape形状的数组\n",
    "\n",
    "a = np.array([1,2,3,4,5,6,7,8])\n",
    "b = a.reshape((2,4))\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a.resize()作用跟a.reshape一致\n",
    "# 但是改变原数组\n",
    "a = np.array([1,2,3,4,5,6,7,8])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [5, 6, 7, 8]])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.resize((2,4))\n",
    "a\n",
    "#可以看到a已经改变了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 2, 3, 4],\n",
       "        [5, 6, 7, 8]]),\n",
       " array([[1, 5],\n",
       "        [2, 6],\n",
       "        [3, 7],\n",
       "        [4, 8]]))"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# a.swapaxes(0,1)\n",
    "# 将a的第 0 和第 1 维度进行调换\n",
    "a = np.array([\n",
    "    [1,2,3,4],\n",
    "    [5,6,7,8]\n",
    "])\n",
    "\n",
    "a,a.swapaxes(0,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 2, 3, 4],\n",
       "        [5, 6, 7, 8]]),\n",
       " array([1, 2, 3, 4, 5, 6, 7, 8]))"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([\n",
    "    [1,2,3,4],\n",
    "    [5,6,7,8]\n",
    "])\n",
    "# 将a降维为 1 维\n",
    "a,a.flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 2, 3, 4],\n",
       "        [5, 6, 7, 8]]),\n",
       " array([[1., 2., 3., 4.],\n",
       "        [5., 6., 7., 8.]], dtype=float32))"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# new_a = a.astype(new_type)\n",
    "# 生成 a 的拷贝a'，把a’的元素全都修改成新的类型（显然a不变）\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([\n",
    "    [1,2,3,4],\n",
    "    [5,6,7,8]\n",
    "])\n",
    "b = a.astype(np.float32)\n",
    "a,b #注意 a 是没有改变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 2, 3, 4],\n",
       "        [5, 6, 7, 8]]),\n",
       " [[1, 2, 3, 4], [5, 6, 7, 8]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#ndarray转换为list\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([\n",
    "    [1,2,3,4],\n",
    "    [5,6,7,8]\n",
    "])\n",
    "a,a.tolist()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第3节：ndarray数组的操作\n",
    "---\n",
    "\n",
    "**索引**\n",
    "\n",
    "arr = [9 , 3, 2, 5, 6]\n",
    "\n",
    "id1 = [0 , 1, 2, 3, 4]（正索引）\n",
    "\n",
    "id2 = [-5,-4,-3,-2,-1]（负索引）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9 6\n"
     ]
    }
   ],
   "source": [
    "# 索引复习\n",
    "arr = [9,3,2,5,6]\n",
    "print(arr[0],arr[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切片复习\n",
    "# 基本格式a[begin:end:step]\n",
    "# 区间[begin,end)中，每隔step取一个元素\n",
    "#--------------------------------------------#\n",
    "# ：个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7] 1\n",
      "[1 2]\n",
      "[1 3]\n"
     ]
    }
   ],
   "source": [
    "# 0个：直接相当于索引\n",
    "b = np.arange(8)\n",
    "print(b,b[1])\n",
    "\n",
    "# 1个：表示省略step，这时step默认1\n",
    "print(b[1:3])\n",
    "\n",
    "# 2个，这是最标准的写法\n",
    "print(b[1:5:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "# step：正表示“从左往右”取值，负表示“从右往左”取值。当step省略时，默认为1，即从左往右以步长1取值\n",
    "\n",
    "# begin: 起始索引（包含该索引）\n",
    "# 该参数省略时，表示从对象“端点”开始取值，至于是从“起点”还是从“终点”开始\n",
    "# 则由step参数的正负决定，step为正从“起点”开始，为负从“终点”开始。\n",
    "\n",
    "# end：终止索引（不包含该索引）\n",
    "# 该参数省略时，表示一直取到数据“端点”，至于是到“起点”还是到“终点”\n",
    "# 由step参数的正负决定，step为正时直到“终点”，为负时直到“起点”\n",
    "\n",
    "# 具体参考这个链接\n",
    "# https://www.jianshu.com/p/15715d6f4dad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**以上是一维数组的索引/切片复习**\n",
    "\n",
    "下面来看下多维数组的情况\n",
    "\n",
    "---\n",
    "\n",
    "### 多维数组的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n",
      "6\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(8).reshape(2, 4)\n",
    "print(a)\n",
    "\n",
    "# 每个维度一个索引值，逗号分割\n",
    "# 把每一个维度都当作一维数组即可\n",
    "\n",
    "print(a[1,2])\n",
    "print(a[-1,-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "# 每个维度切片方法，与一维数组相同\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(8).reshape(2, 4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对于<某个维度>,没有:表示选取这个维度\n",
    "a[1,1:3] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#一个:表示选取本维度[begin,end)所有元素\n",
    "a[:,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 1, 0],\n",
       "       [7, 6, 5, 4]])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2个: 表示添加步长step\n",
    "a[:,::-1] "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "# 第4节：ndarray数组的运算\n",
    "\n",
    "数组与标量之间的运算：作用于数组的每一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n",
      "[[ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "8.5\n"
     ]
    }
   ],
   "source": [
    "#例如:数组+5 => 数组内每一个元素都+5\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(8).reshape(2, 4)\n",
    "print(a)\n",
    "a += 5\n",
    "print(a)\n",
    "\n",
    "# a.mean()表示数组a的平均值\n",
    "print(a.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 3. 4.] [1. 2. 3.]\n"
     ]
    }
   ],
   "source": [
    "# numpy一元函数\n",
    "\n",
    "# np.ceil(x) np.floor(x) 计算数组各元素的ceiling值 或 floor值\n",
    "a = np.array([1.2,2.5,3.6])\n",
    "# 向上取整/向下取整\n",
    "print(np.ceil(a),np.floor(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 4.]\n",
      "(array([0.2, 0.5, 0.6]), array([1., 2., 3.]))\n"
     ]
    }
   ],
   "source": [
    "# np.rint(x) 计算数组各元素的四舍五入值\n",
    "# np.modf(x) 将数组各元素的小数和整数部分以两个独立数组形式返回\n",
    "a = np.array([1.2,2.5,3.6])\n",
    "print(np.rint(a))\n",
    "print(np.modf(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**常见一元函数举例**\n",
    "![avatar](./image/day1-img4.PNG)\n",
    "![avatar](./image/day1-img5.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 5, 6])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy二元函数\n",
    "import numpy as np\n",
    "\n",
    "a = [1, 4, 6]\n",
    "b = [-1, 5, -3]\n",
    "a, b = np.array(a), np.array(b)\n",
    "\n",
    "np.maximum(a,b) #等价于np.fmax(a,b)\n",
    "#np.mod(a,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----\n",
    "**常见二元函数举例**\n",
    "![avatar](./image/day1-img6.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.1 64-bit",
   "language": "python",
   "name": "python38164bitfe2ebe8b9a4f493e93d81a74c550882b"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
